package com.test.map;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.test.map.HTTP.OKHttp;
import com.test.map.Users.Car;
import com.test.map.sign.BaseActivity;
import com.test.map.sign.DateBaseHelper;

import java.util.ArrayList;

import okhttp3.FormBody;

public class car_sign extends BaseActivity {
    private DateBaseHelper hp;
    private EditText id,type,name,place,colour;
    String account,driver_password,phone;
    private Button sign;
    public OKHttp httpRequest=new OKHttp();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_layout);
        hp=new DateBaseHelper(this,"Users.db",null,1);//数据库的初始化
        sign=(Button)findViewById(R.id.button_car);
        id=(EditText)findViewById(R.id.car_id) ;
        type=(EditText)findViewById(R.id.car_type) ;
        name=(EditText)findViewById(R.id.car_name) ;
        place=(EditText)findViewById(R.id.car_place) ;
        colour=(EditText)findViewById(R.id.car_colour) ;
        ArrayList<String> infoList = new ArrayList<String>();
        infoList = getIntent().getStringArrayListExtra("name");
        account = infoList.get(0);
        driver_password = infoList.get(1);
        phone = infoList.get(2);
        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Car car=init();
                if (CheckIsDataAlreadyInDBorNot(car.getId())) {
                    Toast.makeText(car_sign.this,"该车辆已被注册，注册失败", Toast.LENGTH_SHORT).show();
                }
                else {
                    if (register(car)) {
                        post();
                        Toast.makeText(car_sign.this, "恭喜您 注册成功，即将返回登录界面", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(car_sign.this,MainActivity.class);
                        startActivity(intent);
                    }
                }

            }
        });

    }
    public void post(){
        FormBody body = new FormBody.Builder()
                .add("account",phone)
                .add("name",name.getText().toString())
                .add("password",driver_password)
                .add("carcode",id.getText().toString()+colour.getText().toString())
                .add("cartype",type.getText().toString())
                .build();

        httpRequest.post("http://47.105.179.182:5000/register/driver",body );

    }
    public Car init(){
        Car car = new Car();
        String car_id = id.getText().toString();
        String car_type = type.getText().toString();
        String car_name = name.getText().toString();
        String car_place = place.getText().toString();
        String car_colour = colour.getText().toString();
        car.setId(car_id);
        car.setType(car_type);
        car.setName(car_name);
        car.setPlace(car_place);
        car.setColour(car_colour);

        return car;

    }
    public boolean CheckIsDataAlreadyInDBorNot(String value){
        SQLiteDatabase db=hp.getWritableDatabase();
        String Query = "Select * from carData where id =?";
        Cursor cursor = db.rawQuery(Query,new String[] { value });
        if (cursor.getCount()>0){
            cursor.close();
            return  true;
        }
        cursor.close();
        return false;
    }
    public boolean register(Car car){
        SQLiteDatabase db= hp.getWritableDatabase();
        /*String sql = "insert into userData(name,password) value(?,?)";
        Object obj[]={username,password};
        db.execSQL(sql,obj);*/
        ContentValues values=new ContentValues();
        values.put("id",car.getId());
        values.put("type",car.getColour());
        values.put("colour",car.getColour());
        values.put("working","false");
        values.put("place",car.getPlace());
        db.insert("carData",null,values);
        db.close();
        //db.execSQL("insert into userData (name,password) values (?,?)",new String[]{username,password});
        return true;

    }
}
